package com.zzyl.nursing.service.impl;

import com.zzyl.nursing.entity.HealthData;
import com.zzyl.nursing.service.IHealthService;
import com.zzyl.nursing.vo.HealthCardVo;
import lombok.Data;
import org.apache.poi.xssf.model.CalculationChain;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HealthServiceImpl implements IHealthService {

    private final RestTemplate restTemplate;

    public HealthServiceImpl(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }

    @Override
    public List<HealthData> getHealthDataByIotId(String iotId) {
        //调用接口获取原始数据
        return restTemplate.getForObject(
                "http://localhost:8080"+iotId,
                List.class
        );
    }

    @Override
    public List<HealthCardVo> convertToCardVo(List<HealthData> rawData) {
        List<HealthCardVo> cardList = new ArrayList<>();
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("MM.dd HH:mm");

        for (HealthData data : rawData){
            HealthCardVo vo =new HealthCardVo();
            vo.setTypeName(mapFunctionIdToName(data.getFunction()));
            vo.setValue(data.getValue());
            vo.setTag(calculateTag(data.getFunction(),data.getValue()));

            //格式化时间
            try{
                Date date = inputFormat.parse(data.getEventTime());
                vo.setTime(outputFormat.format(date));
            }catch(ParseException e){
                vo.setTime(data.getEventTime());
            }
            cardList.add(vo);
        }
            return cardList;
    }

    // 映射 functionId 到中文名称（Java 11 传统 switch）
    private String mapFunctionIdToName(String functionId) {
        String typeName;
        switch (functionId) {
            case "HeartRate":
                typeName = "心率";
                break;
            case "BloodPressure":
                typeName = "血压";
                break;
            case "BloodOxygen":
                typeName = "血氧";
                break;
            case "BodyTemperature":
                typeName = "体温";
                break;
            default:
                typeName = "未知数据";
                break;
        }
        return typeName;
    }
    // 计算标签（Java 11 传统 switch）
    private String calculateTag(String functionId, String value) {
        try {
            double val = Double.parseDouble(value.replaceAll("[^0-9.]", ""));

            switch (functionId) {
                case "HeartRate":
                    return val >= 60 && val <= 100 ? "正常" : "异常";
                case "BloodPressure":
                    String[] bp = value.split("/");
                    double high = Double.parseDouble(bp[0].replaceAll("[^0-9.]", ""));
                    double low = Double.parseDouble(bp[1].replaceAll("[^0-9.]", ""));
                    return high >= 90 && high <= 140 && low >= 60 && low <= 90 ? "正常" : "异常";
                case "BloodOxygen":
                    return val >= 95 ? "正常" : "偏低";
                case "BodyTemperature":
                    return val >= 36 && val <= 37.2 ? "正常" : "异常";
                default:
                    return "未知";
            }
        } catch (Exception e) {
            return "未知";
        }
    }
}
